﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Frame.Pages;
using System.Data.SqlClient;

public partial class AnalysisReport_DishesSalesAnalysis : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        Frame.Global.GlobalFunctions.SetGridView(this.gridView, false);
        if (!IsPostBack && !IsCallback)
        {
            string sql = @"select a.Dish_ID,
                                  b.Value,
                                  b.DishName,
                                  c.TypeName, 
                                  SUM(a.[Count]) as TotalSales
                           from   dbo.BLI_ConsumerHisList a,
                                  dbo.BI_Dish b,
                                  dbo.BI_DishType c 
                           where  a.Dish_ID=b.DishID and 
                                  b.Type_ID=c.DishTypeID 
                           group by a.Dish_ID,
                                    b.DishName,
                                    b.Value,
                                    c.TypeName
                           order by TotalSales desc";
            gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(sql);
            gridView.DataBind();

            datStart.Date = DateTime.Today;
            datEnd.Date = DateTime.Today;
            Session["sqlstr"] = sql;
            Session["sqlParams"] = null;
        }

        DDataBind();
    }

    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["sqlstr"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Session["sqlstr"].ToString(), Session["sqlParams"] as SqlParameter[]);
            }
            return table;
        }
    }

    void DDataBind()
    {
        if (Session["sqlstr"] != null)
        {
            gridView.DataSource = Table;
            gridView.DataBind();
        }
    }

    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (datStart.Date.Subtract(datEnd.Date).TotalDays > 0)
        {
            throw new Exception("结束时间必须大于开始时间！");
        }

        string sql = @"select     a.Dish_ID,
                                  b.Value,
                                  b.DishName,
                                  c.TypeName, 
                                  SUM(a.[Count]) as TotalSales
                           from   dbo.BLI_ConsumerHisList a,
                                  dbo.BI_Dish b,
                                  dbo.BI_DishType c 
                           where  a.Dish_ID=b.DishID and 
                                  b.Type_ID=c.DishTypeID and
                                  a.ConsumeNo in (select ConsumNo 
                                                  from   dbo.BLI_ConsumerHisDetail 
                                                  where  BookNo 
                                                  in (select BookNo 
                                                      from   dbo.BLI_ConsumerHis 
                                                      where  ConsumerDate >=@BeginDate and
                                                             ConsumerDate <@EndDate) )
                           group by a.Dish_ID,
                                    b.DishName,
                                    b.Value,
                                    c.TypeName
                           order by TotalSales desc";
        List<SqlParameter> Params = new List<SqlParameter>();
        Params.Add(new SqlParameter("@BeginDate", datStart.Date));
        Params.Add(new SqlParameter("@EndDate", datEnd.Date.AddHours(23).AddMinutes(59).AddSeconds(59)));
        Session["sqlstr"] = sql;
        Session["sqlParams"] = Params.ToArray();
        gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(sql, Params.ToArray());
        gridView.DataBind();
    }

    protected override string ActionID
    {
        get { return "0"; }
    }
}